package com.ledim.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.ledim.activity.base.LeDimBaseActivity;
import com.ledim.bean.LedimUserBean;
import com.ledim.bean.base.BaseResultDataInfo;
import com.ledim.service.ApiService;
import com.ledim.service.LedimApiServiceFactory;
import com.ledim.service.LedimHttpObserver;
import com.ledim.service.coverter.LedimGsonConverterFactory;
import com.ledim.utils.LedimSpUtils;
import com.ledim.utils.ToastUtil;
import com.ledim.widget.view.LoadingBar;
import com.letv.android.young.client.R;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 修改用户昵称
 */
public class UserNameActivity extends LeDimBaseActivity implements View.OnClickListener {

    public static final String USER_NAME = "user_name";
    public static final int REQUESTCODE_USER_NAME = 201;
    private ImageView mBack;
    private TextView mFinish;
    private EditText mName;
    private LedimUserBean mUser;
    private String mNameStr;
    private LoadingBar mLoading;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_user_name;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        mUser = (LedimUserBean) getIntent().getSerializableExtra(USER_NAME);
        mBack = (ImageView) findViewById(R.id.user_name_back);
        mFinish = (TextView) findViewById(R.id.user_name_finish);
        mName = (EditText) findViewById(R.id.user_name_content);
        mBack.setOnClickListener(this);
        mFinish.setOnClickListener(this);
        if (mUser.nickname != null) {
            mName.setText(mUser.nickname);
            mName.requestFocus();
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.user_name_back:
                finish();
                break;
            case R.id.user_name_finish:
                mNameStr = mName.getText().toString().trim();
                if (mNameStr == null || mNameStr.length() == 0) {
                    ToastUtil.toast("请输入用户昵称");
                } else if (!mNameStr.matches("^[\\u4e00-\\u9fa5_a-zA-Z0-9]+$")) {
                    ToastUtil.toast("只支持中英文，数字和“_”，火星文和颜文字现实功能开发进展中……");
                } else if (mNameStr.matches("^[0-9]+$") || mNameStr.matches("^[_]+$")) {
                    ToastUtil.toast("昵称不能只有数字或者只有下划线");
                } else {
                    modifyProfileName(mNameStr);
                }

                break;
        }
    }

    /**
     * 修改用户姓名
     */
    private void modifyProfileName(String name) {

        if (mLoading == null) {
            mLoading = new LoadingBar(this);
        }
        mLoading.show();
        LedimApiServiceFactory.createService(ApiService.class)
                              .modifyProfileName(name)
                              .observeOn(AndroidSchedulers.mainThread())
                              .subscribeOn(Schedulers.io())
                              .subscribe(new LedimHttpObserver<BaseResultDataInfo<LedimUserBean>>() {
                                  @Override
                                  public void onCompleted() {
                                      super.onCompleted();
                                  }

                                  @Override
                                  public void onError(Throwable e) {
                                      super.onError(e);
                                      mLoading.dismiss();
                                      ToastUtil.toast(e.getMessage());
                                  }

                                  @Override
                                  public void onNext(BaseResultDataInfo<LedimUserBean> data) {
                                      super.onNext(data);
                                      mLoading.dismiss();
                                      if (data != null) {
                                          if (data.success) {
                                              LedimSpUtils.setUser(LedimGsonConverterFactory.create().gson
                                                      .toJson(data.data));
                                              ToastUtil.toast("用户昵称修改成功");
                                              Intent intent = new Intent();
                                              intent.putExtra("content", mNameStr);
                                              // 设置结果，并进行传送
                                              setResult(REQUESTCODE_USER_NAME, intent);
                                              finish();
                                          } else {
                                              //登录失败
                                              ToastUtil.toast(data.message);
                                          }
                                      }
                                  }
                              });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mLoading != null) {
            mLoading.dismiss();
        }
    }
}
